﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NCommon;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Padb22")]
    public class Padb22Controller : ApiController
    {   

        [HttpPost]
        [Route("PostPadb22")]

        //多记录查询
        public System.Data.DataSet PostPadb22([FromBody]  JObject obj)
        {

          

            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();
          

            inBlock.Tables[0].Columns.Add("QUESTION");
            inBlock.Tables[0].Columns.Add("ASMAT_NAME");
            inBlock.Tables[0].Columns.Add("DEFECT_NAME");//异议类型
            inBlock.Tables[0].Columns.Add("BILL_STATUS");
            inBlock.Tables[0].Columns.Add("APPLY_BILL_NO");    //申请单号    
            inBlock.Tables[0].Columns.Add("BRANCH_CODE");  //反馈公司
            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");
            inBlock.Tables[0].Columns.Add("BILL_TYPE");        //入账状态
            inBlock.Tables[0].Columns.Add("STOCK_NO"); 
            inBlock.Tables[0].Columns.Add("SUPPLIER_CODE");
            inBlock.Tables[0].Columns.Add("SUPPLIER_NAME");
            inBlock.Tables[0].Columns.Add("DPTMENT_CODE");   

            inBlock.Tables[0].Rows.Add();
            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid = obj["USERID"].ToString();
            inBlock.sys_info.formname = "PADB29"; //前台画面名称； 查询未结案天数

            inBlock.Tables[0].Rows[0]["QUESTION"]           =  obj["QUESTION"].ToString();
            inBlock.Tables[0].Rows[0]["ASMAT_NAME"]         =  obj["ASMAT_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["DEFECT_NAME"]        =  obj["DEFECT_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["BILL_STATUS"]        =  obj["BILL_STATUS"].ToString();
            inBlock.Tables[0].Rows[0]["APPLY_BILL_NO"]      =  obj["APPLY_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["BRANCH_CODE"]        =  obj["BRANCH_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"]    =  obj["REC_CREATE_TIME"].ToString();
            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"]    =  obj["REC_REVISE_TIME"].ToString();
            inBlock.Tables[0].Rows[0]["BILL_TYPE"]          =  obj["BILL_TYPE"].ToString();
            inBlock.Tables[0].Rows[0]["STOCK_NO"]           =  obj["STOCK_NO"].ToString();
            inBlock.Tables[0].Rows[0]["SUPPLIER_CODE"]      =  obj["SUPPLIER_CODE"].ToString();
            inBlock.Tables[0].Rows[0]["SUPPLIER_NAME"]      =  obj["SUPPLIER_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["DPTMENT_CODE"]       =  obj["DPTMENT_CODE"] == null ? " " : obj["DPTMENT_CODE"].ToString();
            

         


            EI.EIInfo outBlock = EI.EITuxedo.CallService("padb22_inq", inBlock);

            //小代码转换；
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PAA8","PAA9","PAAG","FIBR","FIDP");
            DataTable dtTemp = null;

        

            for (int i = outBlock.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                //单据状态
                string BILL_STATUS = outBlock.Tables[0].Rows[i]["BILL_STATUS"].ToString().Trim();
                if (BILL_STATUS.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAA8"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                //入账状态
                string BILL_TYPE = outBlock.Tables[0].Rows[i]["BILL_TYPE"].ToString().Trim();
                if (BILL_TYPE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAA9"].Select("CODE  = '" + BILL_TYPE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["BILL_TYPE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                //质量异议类型 
                string DEFECT_NAME = outBlock.Tables[0].Rows[i]["DEFECT_NAME"].ToString().Trim();
                if (DEFECT_NAME.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAAG"].Select("CODE  = '" + DEFECT_NAME + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["DEFECT_NAME"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                
                 //BRANCH_CODE 反馈分公司 
                string BRANCH_CODE = outBlock.Tables[0].Rows[i]["BRANCH_CODE"].ToString().Trim();
                if (BRANCH_CODE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + BRANCH_CODE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                // 反馈分厂 FIDP
                string DPTMENT_CODE = outBlock.Tables[0].Rows[i]["DPTMENT_CODE"].ToString().Trim();
                if (DPTMENT_CODE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + DPTMENT_CODE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                //申报部门 FIDP
                string CHECK_DPTMENT_CODE = outBlock.Tables[0].Rows[i]["CHECK_DPTMENT_CODE"].ToString().Trim();
                if (CHECK_DPTMENT_CODE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + CHECK_DPTMENT_CODE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["CHECK_DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                 




                string RECEIVE_DATE = outBlock.Tables[0].Rows[i]["RECEIVE_DATE"].ToString();
                if (RECEIVE_DATE.Length>=8)
                    outBlock.Tables[0].Rows[i]["RECEIVE_DATE"] = RECEIVE_DATE.Substring(0, 4) + "-" + RECEIVE_DATE.Substring(4, 2) + "-" + RECEIVE_DATE.Substring(6, 2);



                string apply_date = outBlock.Tables[0].Rows[i]["REC_CREATE_TIME"].ToString();
                outBlock.Tables[0].Rows[i]["REC_CREATE_TIME"] = apply_date.Substring(0, 4) + "-" + apply_date.Substring(4, 2) + "-" + apply_date.Substring(6, 2);


            }

            
            
            outBlock.Tables[0].TableName = "Table0";


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


        [HttpPost]
        [Route("PostPadb22_Info")]

        //单记录查询
        public System.Data.DataSet PostPadb22_Info([FromBody]  JObject obj)
        {


            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            string apply_bill_no = obj["APPLY_BILL_NO"].ToString();

            EI.EIInfo outBlock = new EI.EIInfo();
            //1***************************************************************************************跟踪履历
            string sql_str = " select  ";
            sql_str  +=" to_char(to_date(rec_create_time,'yyyymmddhh24miss'),'yyyy-mm-dd') REC_CREATE_TIME ";
            sql_str  +=" ,REC_CREATOR,REMARK1 ";
            sql_str  +=" from tpabm29 t where  ROW_GUID='" + apply_bill_no + "'";
            sql_str += " order by t.REC_CREATE_TIME desc  ";
            EI.EIInfo outBlock1 = EF.Utility.ExecQuery(sql_str);
            outBlock1.Tables[0].TableName = "TPABM29";


            

            //2***************************************************************************************附件
            //协议主表：TPADB20,协议子表；TPADB21 tb 


            sql_str     =   " select  ta.* ,tb.*   from TPADB20 ta, TPADB21 tb ";
            sql_str     +=  "  where ta.bill_no = tb.bill_no  ";
            sql_str     +=  "   and tb.APPLY_BILL_NO='" + apply_bill_no + "'  ";
           

            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(sql_str);
            outBlock2.Tables[0].TableName = "TPADB21";

            //即使为空，也应压人前台；
            /*
             目前前台仅支持图片，各种图片格式都可以，但不支持word、excle 、pdf等格式，浏览时是黑屏，不影响             
             */



            outBlock2.Tables.Add("FILES");
            outBlock2.Tables["FILES"].Columns.Add("FNAME");

            if (outBlock2.Tables[0].Rows.Count>0)
            {
                string bill_no = outBlock2.Tables[0].Rows[0]["BILL_NO"].ToString();

                MyFft ftp = new MyFft("_PADB/");
                string[] str = ftp.LisFiles(bill_no);
                if (str != null)
                { 
                    for (int i = 0; i < str.Length; i++)
                    {
                        outBlock2.Tables["FILES"].Rows.Add("http://58.210.172.37:8019/_PADB/" + bill_no + "/" + str[i].ToString());                      
                    } 
                }
            }











           


            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PAAD", "PAAE", "PAAF", "PAAC");
            DataTable dtTemp = null;

            for (int i = outBlock2.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                 
                string BILL_TYPE = outBlock2.Tables[0].Rows[i]["BILL_TYPE"].ToString().Trim();
                if (BILL_TYPE.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAAC"].Select("CODE  = '" + BILL_TYPE + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock2.Tables[0].Rows[i]["BILL_TYPE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                 
                
                string BILL_STATUS = outBlock2.Tables[0].Rows[i]["BILL_STATUS"].ToString().Trim();
                if (BILL_STATUS.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAAD"].Select("CODE  = '" + BILL_STATUS + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock2.Tables[0].Rows[i]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                 


                string POST_PROC = outBlock2.Tables[0].Rows[i]["POST_PROC"].ToString().Trim();
                if (POST_PROC.Trim() != "")
                {
                    POST_PROC = "'" + POST_PROC.Replace(", ", "','") + "'";

                    dtTemp = outBlock_TEMP.Tables["PAAE"].Select("CODE  in  (" + POST_PROC + ")").CopyToDataTable<DataRow>();
                    for (int k=0;k<dtTemp.Rows.Count;k++)
                    {
                        if (k==0)
                        {
                            outBlock2.Tables[0].Rows[i]["POST_PROC"] = dtTemp.Rows[k]["CODE_DESC_1_CONTENT"];
                        }
                        else
                        {
                            outBlock2.Tables[0].Rows[i]["POST_PROC"] = outBlock2.Tables[0].Rows[i]["POST_PROC"].ToString() + "," + dtTemp.Rows[k]["CODE_DESC_1_CONTENT"].ToString();
                        }
                    }
                }


                string DEPT_NAME = outBlock2.Tables[0].Rows[i]["DEPT_NAME"].ToString().Trim();
                if (DEPT_NAME.Trim() != "")
                {
                    DEPT_NAME = "'" + DEPT_NAME.Replace(", ", "','") + "'";

                    dtTemp = outBlock_TEMP.Tables["PAAF"].Select("CODE  in  (" + DEPT_NAME + ")").CopyToDataTable<DataRow>();
                    for (int k = 0; k < dtTemp.Rows.Count; k++)
                    {
                        if (k == 0)
                        {
                            outBlock2.Tables[0].Rows[i]["DEPT_NAME"] = dtTemp.Rows[k]["CODE_DESC_1_CONTENT"];
                        }
                        else
                        {
                            outBlock2.Tables[0].Rows[i]["DEPT_NAME"] = outBlock2.Tables[0].Rows[i]["DEPT_NAME"].ToString() + "," + dtTemp.Rows[k]["CODE_DESC_1_CONTENT"].ToString();
                        }
                    }
                }

                
                   
               


                string CONT_SIGN_DATE = outBlock2.Tables[0].Rows[i]["CONT_SIGN_DATE"].ToString();
                if (CONT_SIGN_DATE.Length >= 8)
                    outBlock2.Tables[0].Rows[i]["CONT_SIGN_DATE"] = CONT_SIGN_DATE.Substring(0, 4) + "-" + CONT_SIGN_DATE.Substring(4, 2) + "-" + CONT_SIGN_DATE.Substring(6, 2);



            }


         



            //3***************************************************************************************执行结果
            //执行结果
               sql_str  = " select    ta.REC_REVISOR ";
               sql_str += " ,decode(length(ta.REC_REVISE_TIME),14, to_char(to_date(ta.REC_REVISE_TIME,'yyyymmddhh24miss'),'yyyy-mm-dd'),' ') REC_REVISE_TIME ";
               sql_str += " ,ta.REMARK1,tb.code_desc_1_content  DPTMENT_CODE  ";
               sql_str += " from TPADB23  ta ,(select * from tep0002 where code_class= 'PAAF') tb";
               sql_str += " where ta.DPTMENT_CODE  = tb.code ";
               sql_str += " and ta.APPLY_BILL_NO='" + apply_bill_no + "'";
               sql_str += "  order by ta.REC_CREATE_TIME desc ";
               EI.EIInfo outBlock3 = EF.Utility.ExecQuery(sql_str);
               outBlock3.Tables[0].TableName = "TPADB23";

            
               
 


            outBlock.Tables.Add(outBlock1.Tables[0].Copy());
            outBlock.Tables.Add(outBlock2.Tables[0].Copy());
            outBlock.Tables.Add(outBlock2.Tables[1].Copy());
            outBlock.Tables.Add(outBlock3.Tables[0].Copy());



            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }



        [HttpPost]
        [Route("PostPadb22_inq")]

        //高级查询的查询条件获取，【主要是各类小代码，类似之前画面的初始化参数】
        public System.Data.DataSet PostPadb22_inq([FromBody]  JObject obj)
        {

            //小代码转换； , "FIDP"
            EI.EIInfo outBlock = EF.Utility.GetCodeClassValue("PAA8", "PAA9", "PAAG", "FIBR");

            DataRow dr1 = outBlock.Tables["PAA8"].NewRow();
            dr1["CODE"] = " ";
            dr1["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["PAA8"].Rows.InsertAt(dr1,0);


            DataRow dr2 = outBlock.Tables["PAA9"].NewRow();
            dr2["CODE"] = " ";
            dr2["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["PAA9"].Rows.InsertAt(dr2, 0);

            DataRow dr3 = outBlock.Tables["PAAG"].NewRow();
            dr3["CODE"] = " ";
            dr3["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["PAAG"].Rows.InsertAt(dr3, 0);

            DataRow dr4 = outBlock.Tables["FIBR"].NewRow();
            dr4["CODE"] = " ";
            dr4["CODE_DESC_1_CONTENT"] = " ";
            outBlock.Tables["FIBR"].Rows.InsertAt(dr4, 0);

            //DataRow dr5 = outBlock.Tables["FIDP"].NewRow();
            //dr5["CODE"] = " ";
            //dr5["CODE_DESC_1_CONTENT"] = " ";
            //outBlock.Tables["FIDP"].Rows.InsertAt(dr5, 0);



            string str ="select distinct BRANCH_CODE, a.DPTMENT_CODE as code, b.CODE_DESC_1_CONTENT ";
            str +=" from tsifiaa71 a, tep0002 b  ";
            str +=" where  b.code_class = 'FIDP' ";
            str +=" and a.DPTMENT_CODE = b.code  ";
            str +=" and b.code_desc_5_content in ('2', '3') ";
            str +=" order by a.dptment_code " ;

            EI.EIInfo outBlock2 = EF.Utility.ExecQuery(str);

            outBlock2.Tables[0].TableName = "FIDP";


            outBlock.Tables.Add(outBlock2.Tables[0].Copy());

 
   
    
 

          

          
 

            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        [HttpPost]
        [Route("PostPadb22_pro")]

        //审核、驳回、审核撤销
        public System.Data.DataSet PostPadb22_pro([FromBody]  JObject obj)
        {



            //obj对象区分大小写的； obj["ASMAT_NAME"] 报错。
            EI.EIInfo inBlock = new EI.EIInfo();


         


            //系统信息；
            //inBlock.Tables.Add("SYS");
            //inBlock.Tables["SYS"].Columns.Add("FLAG");
            //inBlock.Tables["SYS"].Columns.Add("MSG");
            //inBlock.Tables["SYS"].Rows.Add(-1, "error outBlock.sys_info.msg");
            //return inBlock;
          
           
            inBlock.Tables[0].Columns.Add("APPLY_BILL_NO");    //申请单号   
            inBlock.Tables[0].Columns.Add("SCHEME_NAME");
            inBlock.Tables[0].Columns.Add("APPLY_REASON");

  

            inBlock.Tables[0].Rows.Add();
            inBlock.sys_info.company_code   =   "LT";
            inBlock.sys_info.userid         =   obj["USERID"].ToString();
            inBlock.sys_info.username       =   obj["USERNAME"].ToString();

            inBlock.sys_info.formname       =   obj["FORMNAME"].ToString(); 

           
            inBlock.Tables[0].Rows[0]["APPLY_BILL_NO"]  = obj["APPLY_BILL_NO"].ToString();
            inBlock.Tables[0].Rows[0]["SCHEME_NAME"]    = obj["SCHEME_NAME"].ToString();
            inBlock.Tables[0].Rows[0]["APPLY_REASON"] = obj["APPLY_REASON"].ToString();

           


            inBlock.Tables[0].TableName = obj["ACTION"].ToString();
            EI.EIInfo outBlock = EI.EITuxedo.CallService("padb22_pro", inBlock);



            


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }

    }
}